package com.nicolasprof.profgallery.effects {
	import gs.TweenLite;

	import com.gskinner.utils.Rndm;
	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.effects.Effect;

	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;

	/**
	 * @author Nicolas Prof
	 */
	public class FadeWaveEffect extends Effect {

		public static const AVAILABLE : Array = ["toleft", "toright", "totop", "tobottom"];

		public function FadeWaveEffect() {
			super();
		}
		override public function build(...$layers) : void {
			var bitmap : Bitmap = $layers[0];
			var width : Number = bitmap.width;
			var height : Number = bitmap.height;
			var mask : Sprite = $layers[1];
			var config: XML = getConfig();
			var direction: String = String(config.@direction).toLowerCase();
			
			
			bitmap.cacheAsBitmap = true;
			mask.cacheAsBitmap = true;
			bitmap.mask = mask;
			
			if(FadeWaveEffect.AVAILABLE.indexOf(direction) != -1){
				this[direction](config, width, height, mask);
			}else if(direction == "random") {
				Rndm.seed = Math.random()*0xFFFFFF;
				this[FadeWaveEffect.AVAILABLE[Rndm.integer(4)]](config, width, height, mask);
			}
		}		
		protected function toleft($config: XML, $width: int, $height: int, $mask: Sprite) : void {
			var length : Number = Number($config.@length);
			var ratio : Number = length / (length + $width) * 255;
			var matrix : Matrix = new Matrix();
			var alphas: Array = [0,1,1];
			var colors : Array = [0xFF0000, 0xFF0000, 0xFF0000];
			var ratios : Array = [0,ratio,255];
			
			$mask.x = $width;
			matrix.createGradientBox((length + $width), $height);
			$mask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
			$mask.graphics.drawRect(0, 0, (length + $width), $height);
			$mask.graphics.endFill();
			
			TweenLite.to($mask, $config.@time, {x:-length});
		}
		protected function toright($config: XML, $width: int, $height: int, $mask: Sprite) : void {
			var length : Number = Number($config.@length);
			var ratio : Number = length / (length + $width) * 255;
			var matrix : Matrix = new Matrix();
			var alphas: Array = [0,1,1];
			var colors : Array = [0xFF0000, 0xFF0000, 0xFF0000];
			var ratios : Array = [0,ratio,255];
			
			$mask.x = -($width + length);
			matrix.createGradientBox((length + $width), $height, Math.PI);
			$mask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
			$mask.graphics.drawRect(0, 0, (length + $width), $height);
			$mask.graphics.endFill();
			
			TweenLite.to($mask, $config.@time, {x:0});
		}
		protected function tobottom($config: XML, $width: int, $height: int, $mask: Sprite) : void {
			var length : Number = Number($config.@length);
			var ratio : Number = length / (length + $height) * 255;
			var matrix : Matrix = new Matrix();
			var alphas: Array = [0,1,1];
			var colors : Array = [0xFF0000, 0xFF0000, 0xFF0000];
			var ratios : Array = [0,ratio,255];
			
			$mask.y = -($height + length);
			matrix.createGradientBox($width, (length + $height), -Math.PI*0.5);
			$mask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
			$mask.graphics.drawRect(0, 0, $width, (length + $height));
			$mask.graphics.endFill();
			
			TweenLite.to($mask, $config.@time, {y:0});
		}
		protected function totop($config: XML, $width: int, $height: int, $mask: Sprite) : void {
			var length : Number = Number($config.@length);
			var ratio : Number = length / (length + $height) * 255;
			var matrix : Matrix = new Matrix();
			var alphas: Array = [0,1,1];
			var colors : Array = [0xFF0000, 0xFF0000, 0xFF0000];
			var ratios : Array = [0,ratio,255];
			
			$mask.y = $height;
			matrix.createGradientBox($width, (length + $height), Math.PI*0.5);
			$mask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
			$mask.graphics.drawRect(0, 0, $width, (length + $height));
			$mask.graphics.endFill();
			
			TweenLite.to($mask, $config.@time, {y:-length});
		}

		
		
		override protected function getConfig() : XML {
			var config : XML = Engine.CONFIG_XML;
			return config.effects.fadeWave[0];
		}
	}
}
